//
//  RootViewController.m
//  SoupApp
//
//  Created by pro2 on 2018/11/1.
//  Copyright © 2018 pro2. All rights reserved.
//

#import "RootViewController.h"

@interface RootViewController ()


@property (strong,nonatomic) UIButton *backBtn;
@property (assign,nonatomic) BOOL tabBarIsShow;

@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    //消除导航控制器对scrollView影响
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self.view addSubview:self.naviView];
}

- (void)setTitle:(NSString *)title{
    [super setTitle:title];
    self.naviTitle.text = title;
}

- (void)setNaviViewHidden:(BOOL)hidden{
    self.naviView.hidden = hidden;
}

- (void)hiddenBackBtn{
    self.backBtn.hidden = YES;
}

- (void)setNavBackgroundColor:(UIColor *)color txtColoc:(UIColor *)txtColor
{
    self.naviView.backgroundColor = color;
    self.naviTitle.textColor = txtColor;
}

//设置右边按钮
- (void)setRightButtonWithTitle:(NSString *)title
                          image:(UIImage *)image
                         himage:(UIImage *)hImage
                         action:(SEL)action
{
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightBtn setImage:image forState:UIControlStateNormal];
    [rightBtn setImage:hImage forState:UIControlStateHighlighted];
    [rightBtn setTitle:title forState:UIControlStateNormal];
    [rightBtn setTitleColor:UIColorMakeWithHex(@"#202020") forState:UIControlStateNormal];
    [rightBtn.titleLabel setFont:[UIFont systemFontOfSize:14]];
    [self.naviView addSubview:rightBtn];
    [rightBtn setFrame:CGRectMake(SCREEN_WIDTH - 70, NavigationContentTopConstant - NavigationBarHeight, 60, NavigationBarHeight)];
    [rightBtn addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
}

/**
 设置右边2个按钮
 */
- (void)setRightButtonWithTitle:(NSString *)title
                          image:(UIImage *)image
                         himage:(UIImage *)hImage
                         action:(SEL)action
                       subtitle:(NSString *)subtitle
                       subimage:(UIImage *)subimage
                      subhimage:(UIImage *)subhimage
                      subaction:(SEL)subaction
{
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightBtn setImage:image forState:UIControlStateNormal];
    [rightBtn setImage:hImage forState:UIControlStateHighlighted];
    [rightBtn setTitle:title forState:UIControlStateNormal];
    [rightBtn setTitleColor:UIColorMakeWithHex(@"#202020") forState:UIControlStateNormal];
    [rightBtn.titleLabel setFont:[UIFont systemFontOfSize:14]];
    [self.naviView addSubview:rightBtn];
    [rightBtn setFrame:CGRectMake(SCREEN_WIDTH - 44, NavigationContentTopConstant - NavigationBarHeight, 26, NavigationBarHeight)];
    [rightBtn addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *subBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [subBtn setImage:subimage forState:UIControlStateNormal];
    [subBtn setImage:subhimage forState:UIControlStateHighlighted];
    [subBtn setTitle:subtitle forState:UIControlStateNormal];
    [subBtn setTitleColor:UIColorMakeWithHex(@"#202020") forState:UIControlStateNormal];
    [subBtn.titleLabel setFont:[UIFont systemFontOfSize:14]];
    [self.naviView addSubview:subBtn];
    [subBtn setFrame:CGRectMake(SCREEN_WIDTH - 90, NavigationContentTopConstant - NavigationBarHeight, 26, NavigationBarHeight)];
    [subBtn addTarget:self action:subaction forControlEvents:UIControlEventTouchUpInside];
    
}



- (instancetype)initWithCoder:(NSCoder *)aDecoder{
    self = [super initWithCoder:aDecoder];
    if(self){
        //        self.navigationController.navigationBar.hidden = YES;
    }
    return self;
}


- (void)setShandownColor:(UIColor *)shandownColor{
    _shandownColor = shandownColor;
    if (_shandownColor) {
        self.naviView.layer.shadowColor = _shandownColor.CGColor;
    }
}

- (UIView *)naviView{
    if(!_naviView){
        _naviView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, NavigationContentTopConstant)];
        _naviView.backgroundColor = [UIColor whiteColor];
        [_naviView addSubview:self.naviTitle];
        [_naviView addSubview:self.backBtn];
        _naviView.layer.shadowOpacity = 0.7;
        _naviView.layer.shadowColor = self.shandownColor ? self.shandownColor.CGColor : [UIColor whiteColor].CGColor;
    }
    return _naviView;
}

- (UILabel *)naviTitle{
    if (!_naviTitle) {
        _naviTitle = [[UILabel alloc] initWithFrame:CGRectMake(44, NavigationContentTopConstant - NavigationBarHeight , SCREEN_WIDTH - 88, NavigationBarHeight)];
        _naviTitle.textColor = UIColorMakeWithHex(@"#202020");
        _naviTitle.font = [UIFont fontWithName:@"PingFangSC-Reguler" size: 16];
        _naviTitle.textAlignment = NSTextAlignmentCenter;
        _naviTitle.backgroundColor = UIColorMakeWithRGBA(0, 0, 0, 0);
    }
    return _naviTitle;
}

- (UIButton *)backBtn{
    if (!_backBtn) {
        _backBtn = [[UIButton alloc] initWithFrame:CGRectMake(10, NavigationContentTopConstant - NavigationBarHeight, 44, NavigationBarHeight)];
        [_backBtn setImage:[UIImage imageNamed:@"nav_back_black"] forState:0];
        [_backBtn addTarget:self action:@selector(backBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _backBtn;
}

- (void)backBtnPressed:(UIButton *)sender{
    if (self.navigationController.topViewController == self) {
        [self.navigationController popViewControllerAnimated:YES];
    } else {
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}


@end
